package com.loongtech.bi.action.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.realTime.BossRealTimeManager;
import com.loongtech.bi.manager.realTime.OverviewCountryManager;
import com.loongtech.bi.manager.realTime.OverviewManager;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewData;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * 实时数据-实时概况Action
 *
 * @author Marin
 * @date 2015年09月17日
 */

@Controller("overviewCountryActionOld")
@Scope("view")
//@RestController
public class OverviewCountryAction {
    private Date lookDate = new Date(Time.getBeginningOfDay(Time.currentTime()));  // 前端传过来的查看时间
    private Date compareDate = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的对比时间
    private String queryCountry = "0";  // 前端传过来的查询条件：国家
    private String queryServer = "0";  // 前端传过来的查询条件：区服
    private String queryPlatform = "0";  // 前端传过来的查询条件：平台
    private String queryChannel = "0";  // 前端传过来的查询条件：渠道
    private Map<String, String> countryItmes;  // 国家下拉列表
    private Map<String, String> serverItmes;  // 区服下拉列表


    @SuppressWarnings({"serial"})
    private Map<Integer, String> hourMap = new HashMap<Integer, String>() {
        {
            put(0, "00:00~00:59");
            put(1, "01:00~01:59");
            put(2, "02:00~02:59");
            put(3, "03:00~03:59");
            put(4, "04:00~04:59");
            put(5, "05:00~05:59");
            put(6, "06:00~06:59");
            put(7, "07:00~07:59");
            put(8, "08:00~08:59");
            put(9, "09:00~09:59");
            put(10, "10:00~10:59");
            put(11, "11:00~11:59");
            put(12, "12:00~12:59");
            put(13, "13:00~13:59");
            put(14, "14:00~14:59");
            put(15, "15:00~15:59");
            put(16, "16:00~16:59");
            put(17, "17:00~17:59");
            put(18, "18:00~18:59");
            put(19, "19:00~19:59");
            put(20, "20:00~20:59");
            put(21, "21:00~21:59");
            put(22, "22:00~22:59");
            put(23, "23:00~23:59");
        }
    };
    @SuppressWarnings({"serial"})
    private Map<Integer, String> hourMap1 = new HashMap<Integer, String>() {
        {
            put(0, "00:00~00:59");
            put(1, "00:00~01:59");
            put(2, "00:00~02:59");
            put(3, "00:00~03:59");
            put(4, "00:00~04:59");
            put(5, "00:00~05:59");
            put(6, "00:00~06:59");
            put(7, "00:00~07:59");
            put(8, "00:00~08:59");
            put(9, "00:00~09:59");
            put(10, "00:00~10:59");
            put(11, "00:00~11:59");
            put(12, "00:00~12:59");
            put(13, "00:00~13:59");
            put(14, "00:00~14:59");
            put(15, "00:00~15:59");
            put(16, "00:00~16:59");
            put(17, "00:00~17:59");
            put(18, "00:00~18:59");
            put(19, "00:00~19:59");
            put(20, "00:00~20:59");
            put(21, "00:00~21:59");
            put(22, "00:00~22:59");
            put(23, "00:00~23:59");
        }
    };

    private List<ServerRealTimeManager.OnlineRealTimeData> onlineRealTimeDatas; // 项目整体实时在线数据
    private ChartUIData onlineRealTimeChart; // 项目整体实时在线图表

    private List<OverviewData> datas;  // 前端要显示的实时汇总数据
    private List<OverviewDetailData> detailDatas;  // 前端要显示的详细数据
    private ChartUIData lineChart2;  // 前端要显示的曲线图数据-新增帐号数
    private ChartUIData lineChart20;  // 前端要显示的曲线图数据-去重新增帐号数
    private ChartUIData lineChart3;  // 前端要显示的曲线图数据-活跃帐号数
    private ChartUIData lineChart4;  // 前端要显示的曲线图数据-付费帐号数
    private ChartUIData lineChart5;  // 前端要显示的曲线图数据-付费金额
    private ChartUIData lineChart6;  // 前端要显示的曲线图数据-新增帐号付费数
    private ChartUIData lineChart7;  // 前端要显示的曲线图数据-新增帐号付费金额

    private List<OverviewDetailData> detailDatas1;  // 前端要显示的累计详细数据
    private ChartUIData lineChart21;  // 前端要显示的曲线图数据-新增帐号数
    private ChartUIData lineChart210;  // 前端要显示的曲线图数据-去重新增帐号数
    private ChartUIData lineChart31;  // 前端要显示的曲线图数据-活跃帐号数
    private ChartUIData lineChart41;  // 前端要显示的曲线图数据-付费帐号数
    private ChartUIData lineChart51;  // 前端要显示的曲线图数据-付费金额
    private ChartUIData lineChart61;  // 前端要显示的曲线图数据-新增帐号付费数
    private ChartUIData lineChart71;  // 前端要显示的曲线图数据-新增帐号付费金额

    private String lastUpdateTimeStr = "";  // 前端要访问的最近更新时间字符串


    @Resource
    private OverviewCountryManager overviewCountryManager;
    @Resource
    private OverviewManager overviewManager;
    @Resource
    private BossRealTimeManager bossRealTimeManager;
    @Resource
    private ServerRealTimeManager serverRealTimeManager;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private Session session;

    @PostConstruct
    public void init() {
        initData();
        search();
    }

    // 初始化平台下拉列表
    public void initData() {
        countryItmes = GameParamConfigManager.getItems(CommonGameParam.Nation_Type);
        countryItmes.remove("全部" + CommonGameParam.Nation_Type.DESC);
//        countryItmes = new HashMap<String, String>() {
//            {
//                put("美国", "US");
//                put("法国", "FR");
//                put("德国", "DE");
//                put("阿拉伯联合酋长国", "AE");
//                put("土耳其", "TR");
//                put("罗马尼亚", "RO");
//                put("意大利", "IT");
//            }
//        };
        serverItmes = new LinkedHashMap<>();
        serverItmes.put("全区服", "0");
        for (EntityBiServer p : BiPlatformManager.SERVER_LIST) {
            serverItmes.put(p.getName(), p.getId());
        }
    }

    public void search() {
        sysLogManager.addLog(session.getUserName(), "实时数据-实时概况");

        Date lastWeekDate = new Date(Time.getBeginningOfDay(lookDate.getTime() - 7 * Time.DAY));  // 查看时间前几天的时间
        boolean ignore = Time.getInterval(Time.currentDate(), lookDate) != 0 || Time.getInterval(new Date(Time.currentTime() - Time.DAY), compareDate) != 0;

        lineChart2 = null;
        lineChart20 = null;
        lineChart3 = null;
        lineChart4 = null;
        lineChart5 = null;
        lineChart6 = null;
        lineChart7 = null;

        lineChart21 = null;
        lineChart210 = null;
        lineChart31 = null;
        lineChart41 = null;
        lineChart51 = null;
        lineChart61 = null;
        lineChart71 = null;

        datas = overviewCountryManager.getOverviewData(lookDate, queryPlatform, queryChannel, queryServer, queryCountry);  // 汇总数据
        if ("0".equals(queryServer)) {  // 当查询的为全服的数据时（登录网站默认），更新最近更新时间
            long lastupdatetime = bossRealTimeManager.getMaxUpdateTime();
            lastUpdateTimeStr = "（最近一次更新时间为：" + Time.getCeil1(lastupdatetime) + ")";
        }
        detailDatas = overviewCountryManager.getOverviewDetailData("1", lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);  // 分时详细数据
        if (detailDatas != null && detailDatas.size() > 0) {
            lineChart2 = new ChartUIData();
            lineChart2.setSelectItem("newUserLookValue");
            lineChart20 = new ChartUIData();
            lineChart20.setSelectItem("distinctNewUserLookValue");
            lineChart3 = new ChartUIData();
            lineChart3.setSelectItem("activeUserLookValue");
            lineChart4 = new ChartUIData();
            lineChart4.setSelectItem("payUserLookValue");
            lineChart5 = new ChartUIData();
            lineChart5.setSelectItem("payLookValue");
            lineChart6 = new ChartUIData();
            lineChart6.setSelectItem("newPayUserLookValue");
            lineChart7 = new ChartUIData();
            lineChart7.setSelectItem("newUserPayLookValue");
            if (ignore) {
                lineChart2.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart2.getSelectItem(), "newUserLastWeekValue").loadData(lineChart2.getJsonType()));
                lineChart20.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart20.getSelectItem(), "distinctNewUserLastWeekValue").loadData(lineChart20.getJsonType()));
                lineChart3.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart3.getSelectItem(), "activeUserLastWeekValue").loadData(lineChart3.getJsonType()));
                lineChart4.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart4.getSelectItem(), "payUserLastWeekValue").loadData(lineChart4.getJsonType()));
                lineChart5.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart5.getSelectItem(), "payLastWeekValue").loadData(lineChart5.getJsonType()));
                lineChart6.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart6.getSelectItem(), "newPayUserLastWeekValue").loadData(lineChart6.getJsonType()));
                lineChart7.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart7.getSelectItem(), "newUserPayLastWeekValue").loadData(lineChart7.getJsonType()));
            } else {
                lineChart2.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart2.getSelectItem()).loadData(lineChart2.getJsonType()));
                lineChart20.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart20.getSelectItem()).loadData(lineChart20.getJsonType()));
                lineChart3.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart3.getSelectItem()).loadData(lineChart3.getJsonType()));
                lineChart4.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart4.getSelectItem()).loadData(lineChart4.getJsonType()));
                lineChart5.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart5.getSelectItem()).loadData(lineChart5.getJsonType()));
                lineChart6.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart6.getSelectItem()).loadData(lineChart6.getJsonType()));
                lineChart7.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart7.getSelectItem()).loadData(lineChart7.getJsonType()));
            }
            Collections.sort(detailDatas, overviewManager.new OverviewDetailData().comparatorHour2);
        }

        detailDatas1 = overviewCountryManager.getOverviewDetailData("2", lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);  // 累计详细数据
        if (detailDatas1 != null && detailDatas1.size() > 0) {
            lineChart21 = new ChartUIData();
            lineChart21.setSelectItem("newUserLookValue");
            lineChart210 = new ChartUIData();
            lineChart210.setSelectItem("distinctNewUserLookValue");
            lineChart31 = new ChartUIData();
            lineChart31.setSelectItem("activeUserLookValue");
            lineChart41 = new ChartUIData();
            lineChart41.setSelectItem("payUserLookValue");
            lineChart51 = new ChartUIData();
            lineChart51.setSelectItem("payLookValue");
            lineChart61 = new ChartUIData();
            lineChart61.setSelectItem("newPayUserLookValue");
            lineChart71 = new ChartUIData();
            lineChart71.setSelectItem("newUserPayLookValue");
            if (ignore) {
                lineChart21.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart21.getSelectItem(), "newUserLastWeekValue").loadData(lineChart21.getJsonType()));
                lineChart210.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart210.getSelectItem(), "distinctNewUserLastWeekValue").loadData(lineChart210.getJsonType()));
                lineChart31.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart31.getSelectItem(), "activeUserLastWeekValue").loadData(lineChart31.getJsonType()));
                lineChart41.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart41.getSelectItem(), "payUserLastWeekValue").loadData(lineChart41.getJsonType()));
                lineChart51.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart51.getSelectItem(), "payLastWeekValue").loadData(lineChart51.getJsonType()));
                lineChart61.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart61.getSelectItem(), "newPayUserLastWeekValue").loadData(lineChart61.getJsonType()));
                lineChart71.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart71.getSelectItem(), "newUserPayLastWeekValue").loadData(lineChart71.getJsonType()));
            } else {
                lineChart21.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart21.getSelectItem()).loadData(lineChart21.getJsonType()));
                lineChart210.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart210.getSelectItem()).loadData(lineChart210.getJsonType()));
                lineChart31.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart31.getSelectItem()).loadData(lineChart31.getJsonType()));
                lineChart41.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart41.getSelectItem()).loadData(lineChart41.getJsonType()));
                lineChart51.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart51.getSelectItem()).loadData(lineChart51.getJsonType()));
                lineChart61.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart61.getSelectItem()).loadData(lineChart61.getJsonType()));
                lineChart71.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart71.getSelectItem()).loadData(lineChart71.getJsonType()));
            }

            Collections.sort(detailDatas1, overviewManager.new OverviewDetailData().comparatorHour2);
        }

        onlineRealTimeDatas = new ArrayList<>(); // 项目整体实时在线数据
        onlineRealTimeChart = null; // 项目整体实时在线图表
        // 整体实时在线
        onlineRealTimeDatas = serverRealTimeManager.getOnlineRealTimeDatas(lookDate, compareDate, lastWeekDate, queryPlatform, queryChannel, queryServer);
        if (onlineRealTimeDatas != null && onlineRealTimeDatas.size() > 0) {
            onlineRealTimeChart = new ChartUIData();
            onlineRealTimeChart.setJsonStr(new ChartTool(onlineRealTimeDatas, ServerRealTimeManager.OnlineRealTimeData.class, onlineRealTimeChart.getSelectItem()).loadData(onlineRealTimeChart.getJsonType()));
        } else {
            onlineRealTimeChart = null;
        }
    }

    public String getHourStr(int hour) {
        if (hour < 0 || hour > 23) {
            return "";
        }
        return hourMap.get(hour);
    }

    public String getHourStr1(int hour) {
        if (hour < 0 || hour > 23) {
            return "";
        }
        return hourMap1.get(hour);
    }



    public Date getLookDate() {
        return lookDate;
    }

    public void setLookDate(Date lookDate) {
        this.lookDate = lookDate;
    }

    public Date getCompareDate() {
        return compareDate;
    }

    public void setCompareDate(Date compareDate) {
        this.compareDate = compareDate;
    }

    public String getQueryCountry() {
        return queryCountry;
    }

    public void setQueryCountry(String queryCountry) {
        this.queryCountry = queryCountry;
    }

    public Map<String, String> getCountryItmes() {
        return countryItmes;
    }

    public void setCountryItmes(Map<String, String> countryItmes) {
        this.countryItmes = countryItmes;
    }

    public String getQueryPlatform() {
        return queryPlatform;
    }

    public void setQueryPlatform(String queryPlatform) {
        this.queryPlatform = queryPlatform;
    }

    public String getQueryChannel() {
        return queryChannel;
    }

    public void setQueryChannel(String queryChannel) {
        this.queryChannel = queryChannel;
    }

    public String getQueryServer() {
        return queryServer;
    }

    public void setQueryServer(String queryServer) {
        this.queryServer = queryServer;
    }

    public Map<String, String> getServerItmes() {
        return serverItmes;
    }

    public void setServerItmes(Map<String, String> serverItmes) {
        this.serverItmes = serverItmes;
    }

    public List<OverviewData> getDatas() {
        return datas;
    }

    public void setDatas(List<OverviewData> datas) {
        this.datas = datas;
    }

    public ChartUIData getLineChart2() {
        return lineChart2;
    }

    public void setLineChart2(ChartUIData lineChart2) {
        this.lineChart2 = lineChart2;
    }

    public ChartUIData getLineChart3() {
        return lineChart3;
    }

    public void setLineChart3(ChartUIData lineChart3) {
        this.lineChart3 = lineChart3;
    }

    public ChartUIData getLineChart4() {
        return lineChart4;
    }

    public void setLineChart4(ChartUIData lineChart4) {
        this.lineChart4 = lineChart4;
    }

    public ChartUIData getLineChart5() {
        return lineChart5;
    }

    public void setLineChart5(ChartUIData lineChart5) {
        this.lineChart5 = lineChart5;
    }

    public ChartUIData getLineChart6() {
        return lineChart6;
    }

    public void setLineChart6(ChartUIData lineChart6) {
        this.lineChart6 = lineChart6;
    }

    public ChartUIData getLineChart7() {
        return lineChart7;
    }

    public void setLineChart7(ChartUIData lineChart7) {
        this.lineChart7 = lineChart7;
    }

    public List<OverviewDetailData> getDetailDatas() {
        return detailDatas;
    }

    public void setDetailDatas(List<OverviewDetailData> detailDatas) {
        this.detailDatas = detailDatas;
    }

    public String getLastUpdateTimeStr() {
        return lastUpdateTimeStr;
    }

    public void setLastUpdateTimeStr(String lastUpdateTimeStr) {
        this.lastUpdateTimeStr = lastUpdateTimeStr;
    }

    public List<ServerRealTimeManager.OnlineRealTimeData> getOnlineRealTimeDatas() {
        return onlineRealTimeDatas;
    }

    public void setOnlineRealTimeDatas(List<ServerRealTimeManager.OnlineRealTimeData> onlineRealTimeDatas) {
        this.onlineRealTimeDatas = onlineRealTimeDatas;
    }

    public ChartUIData getOnlineRealTimeChart() {
        return onlineRealTimeChart;
    }

    public void setOnlineRealTimeChart(ChartUIData onlineRealTimeChart) {
        this.onlineRealTimeChart = onlineRealTimeChart;
    }

    public List<OverviewDetailData> getDetailDatas1() {
        return detailDatas1;
    }

    public void setDetailDatas1(List<OverviewDetailData> detailDatas1) {
        this.detailDatas1 = detailDatas1;
    }

    public ChartUIData getLineChart21() {
        return lineChart21;
    }

    public void setLineChart21(ChartUIData lineChart21) {
        this.lineChart21 = lineChart21;
    }

    public ChartUIData getLineChart31() {
        return lineChart31;
    }

    public void setLineChart31(ChartUIData lineChart31) {
        this.lineChart31 = lineChart31;
    }

    public ChartUIData getLineChart41() {
        return lineChart41;
    }

    public void setLineChart41(ChartUIData lineChart41) {
        this.lineChart41 = lineChart41;
    }

    public ChartUIData getLineChart51() {
        return lineChart51;
    }

    public void setLineChart51(ChartUIData lineChart51) {
        this.lineChart51 = lineChart51;
    }

    public ChartUIData getLineChart61() {
        return lineChart61;
    }

    public void setLineChart61(ChartUIData lineChart61) {
        this.lineChart61 = lineChart61;
    }

    public ChartUIData getLineChart71() {
        return lineChart71;
    }

    public void setLineChart71(ChartUIData lineChart71) {
        this.lineChart71 = lineChart71;
    }

    public ChartUIData getLineChart20() {
        return lineChart20;
    }

    public void setLineChart20(ChartUIData lineChart20) {
        this.lineChart20 = lineChart20;
    }

    public ChartUIData getLineChart210() {
        return lineChart210;
    }

    public void setLineChart210(ChartUIData lineChart210) {
        this.lineChart210 = lineChart210;
    }

    public String getColumnwidth(double val, double add) {
        return val > 0 ? PageHelper.double2Percent(val + add) : "0%";
    }
}
